package com.example.aaa;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Instances;
import android.util.Log;
import android.widget.RemoteViews;

public class AAAProvider extends AppWidgetProvider {
	@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.aaa);
            
            Intent updateIntent = new Intent(context, AAAProvider.class);
        	updateIntent.setAction("UPDATE");
            PendingIntent updatePendingIntent = PendingIntent.getBroadcast(context, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            views.setOnClickPendingIntent(R.id.button1, updatePendingIntent);
            
            //views.setTextViewText(R.id.aaa_textview, result);
            
            Intent svcIntent = new Intent(context, WidgetService.class);
            views.setRemoteAdapter(R.id.words, svcIntent);
            
            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
    	}
        
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
     
	@Override
    public void onReceive(Context context, Intent intent) {
		// Toast.makeText(context, intent.getAction(), Toast.LENGTH_SHORT).show();
		
		super.onReceive(context, intent);
		
		if (intent.getAction() == "UPDATE") {
			AppWidgetManager mgr = AppWidgetManager.getInstance(context);
			ComponentName thisWidget = new ComponentName(context, AAAProvider.class);
			int[] appWidgetIds = mgr.getAppWidgetIds(thisWidget);
			onUpdate(context, mgr, appWidgetIds);
		}
    }
}